<template>
  <div class="home">
    <div class="detail_box">
      <!-- 操作 -->
      <div class="operate_box">
        <div class="item_box">
          <div class="operate_item">
            <label>参数值：</label>
            <el-input type="text" placeholder="请输入参数值" clearable v-model="dName"></el-input>
          </div>
          <div class="operate_item">
            <label>类型：</label>
            <el-input type="number" placeholder="请输入事件类型" clearable v-model="eventType"></el-input>
          </div>
          <div class="operate_item">
            <span class="operate_btn search_btn" @click="getTableInfo(1)">查询</span>
            <span class="operate_btn reset_btn" @click="resetTable">重置</span>
          </div>
        </div>
        <div class="btn_box" v-if="btn&&btn.add">
          <span class="operate_btn search_btn" @click="openPopup('')">新增</span>
        </div>
      </div>
      <!-- 表格 -->
      <div class="table_box">
<!--        <el-table-->
<!--          border-->
<!--          v-loading="loading"-->
<!--          :header-cell-style="tableHeader"-->
<!--          ref="multipleTable"-->
<!--          :data="tableInfo"-->
<!--          tooltip-effect="dark"-->
<!--          style="width: 100%;">-->
<!--          <el-table-column-->
<!--            width="80"-->
<!--            prop="id"-->
<!--            label="ID">-->
<!--          </el-table-column>-->
<!--          <el-table-column-->
<!--            width="160"-->
<!--            sortable-->
<!--            prop="modifyTime"-->
<!--            label="修改时间">-->
<!--            <template slot-scope="scope">-->
<!--              <p style="text-align: left">{{formatTime(scope.row.modifyTime)}}</p>-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column-->
<!--            prop="key"-->
<!--            label="键">-->
<!--          </el-table-column>-->
<!--          <el-table-column-->
<!--            prop="value"-->
<!--            label="值">-->
<!--          </el-table-column>-->
<!--          <el-table-column-->
<!--            width="120"-->
<!--            prop="type"-->
<!--            label="类型">-->
<!--          </el-table-column>-->
<!--          <el-table-column-->
<!--            prop="name"-->
<!--            label="类型标注">-->
<!--          </el-table-column>-->
<!--          <el-table-column-->
<!--            width="120"-->
<!--            label="状态">-->
<!--            <template slot-scope="scope">-->
<!--              <div :style="'color:'+(scope.row.enable===1?'#67c430':'#e60000')">-->
<!--                {{scope.row.enable===1?'可用':'禁用'}}-->
<!--              </div>-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column-->
<!--            label="操作"-->
<!--            fixed="right"-->
<!--            v-if="btn&&(btn.update||btn.delete)"-->
<!--            width="150">-->
<!--            <template slot-scope="scope">-->
<!--              <div class="table_btn">-->
<!--                <span class="change_info" @click="openPopup(scope.row.id)" v-if="btn.update">编辑</span>-->
<!--                <span class="change_info delete_btn" @click="deleteItem(scope.row.id)" v-if="btn.delete">删除</span>-->
<!--              </div>-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--        </el-table>-->
        <tableTemplate
          :table-header="tHeader"
          :table-info="tableInfo"
          :loading="loading"
        />
      </div>
      <div class="record_page">
        <span class="page_count">共<i>{{totals}}</i>条</span>
        <el-pagination
          @size-change="sizeChange"
          @current-change="getTableInfo"
          background
          :current-page="currentPage"
          :page-sizes="[15, 20, 30, 40]"
          :page-size="pageSize"
          layout="prev, pager, next, sizes,jumper"
          :total="totals">
        </el-pagination>
      </div>
    </div>
    <edit v-if="isPop" :did="dictId" @listenToChild="listenToEdit"></edit>
  </div>
</template>

<script>

import edit from "./edit";
import commons from "@/utils/common";
import {delData, listData} from "@/api/config/config";
import {getBtn} from "@/utils/btnAuth";
import tableTemplate from '../../../components/tableTemplate/index.vue'

export default {
    components: {
      edit,
      tableTemplate
    },
    data() {
      return {
        tHeader:[
          {
            prop:'id',
            label:'ID',
            width: 80,
            fixed:false,
            template:''
          }, {
            prop:'modifyTime',
            label:'修改时间',
            width: 160,
            fixed:false,
            template:'time'
          },{
            prop:'key',
            label:'键',
            width: 0,
            fixed:false,
            template:''
          },
          {
            prop:'value',
            label:'值',
            width: 0,
            fixed:false,
            template:''
          },{
            prop:'type',
            label:'类型',
            width: 120,
            fixed:false,
            template:''
          },{
            prop:'name',
            label:'类型标注',
            width: 0,
            fixed:false,
            template:''
          },{
            prop:'enable',
            label:'状态',
            width: 120,
            fixed:false,
            template:'status'
          },
          {
            prop:'',
            label:'操作',
            width: 160,
            fixed:true,
            template:getBtn()&&(getBtn().update||getBtn().delete)?'operate':'hide',
            operation:[
              {name: '编辑',type:'edit', clickFun: this.openPopup,limit:getBtn().update},
              {name: '删除',type:'delete', clickFun: this.deleteItem,limit:getBtn().delete},
            ]
          }
        ],
        eventType:'',
        dName:'',
        currentPage: 1,
        pageSize: 15,
        totals: 0,
        loading: false,
        tableInfo:[],
        isPop: false,
        dictId: '',
        firstMenu: '',
        btn: getBtn(),
      }
    },
    mounted() {
      if (this.btn.query){
        this.getTableInfo(1);
      }
    },
    methods: {
      // 获取列表信息
      getTableInfo(page) {
        let that = this;
        that.loading = true;
        that.currentPage = page;
        let queryParams = {
          limit:that.pageSize,
          page:page,
          value:that.dName,
          type:that.eventType
        }
        listData(queryParams).then(res => {
          that.tableInfo = res?res.list:[];
          that.totals = res?res.count:0;
          that.loading = false;
        }).catch((e) => {
          that.loading = false;
        });
      },

      // 重置
      resetTable() {
        this.pageSize = 15;
        this.dName = '';
        this.eventType = '';
        this.getTableInfo(1)
      },

      // 删除
      deleteItem(id) {
        let that = this;
        let table = that.tableInfo;
        that.$confirm('确定删除系统配置信息?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          return delData(id);
        }).then(() => {
          that.getTableInfo(table.length>1||that.currentPage===1?that.currentPage:that.currentPage-1);
          that.$message.success({
            message:'删除成功！',
            duration:1500
          });
        }).catch(() => {
          // console.product('fail')
        });
      },

      // 打开弹窗
      openPopup(id) {
        this.isPop = true;
        this.dictId = id;
      },

      listenToEdit(e){
        this.isPop = false;
        if (e.refresh){
          this.getTableInfo(this.currentPage)
        }
      },

      formatTime(time){
        return commons.formatTime(time)
      },

      sizeChange(val){
        this.pageSize =val;
        this.getTableInfo(1)
      },

      // 设置table标题居中
      tableHeader({row, column, rowIndex, columnIndex}) {
        return 'background:#F0F2F5;color:#111;padding:10px 0;font-size:14px;font-weight:600'
      },
    }
  }
</script>

<style scoped>

</style>
